# -*- coding: utf-8 -*-
import urllib2
import httplib
import re

from bs4 import BeautifulSoup
from app.models import Link


def append_log(message):
    print message

def get_web_page(address):
    try:
        user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        headers = { 'User-Agent' : user_agent }
        request = urllib2.Request(address, None, headers)
        response = urllib2.urlopen(request, timeout=30)
        try:
            return response.read()
        finally:
            response.close()
    except urllib2.HTTPError as e:
        error_desc = httplib.responses.get(e.code, '')
        append_log('HTTP Error: ' + str(e.code) + ': ' +
                  error_desc + ': ' + address)
    except urllib2.URLError as e:
        append_log('URL Error: ' + e.reason[1] + ': ' + address)
    except Exception as e:
        append_log('Unknown Error: ' + str(e) + address)

def get_link_in_div(text, className):
    if text is not None:
        soup = BeautifulSoup(text)

        data = soup.findAll('div',attrs={'class': className});
        linkovi = []
        for div in data:
            links = div.findAll('a')
            name = div.find('a').contents[0]
            for a in links:
                linkovi.append(Link(name, a['href'] ))

        return linkovi
    else:
        pass # do something else

def get_content_in_div_like(text, searchString):
    if text is not None:
        soup = BeautifulSoup(text)

        linews = soup.findAll(True, {'class' : re.compile(searchString)});
        return linews

def clean_text(text):
    if text is not None:
        text = text.replace("\r\n", "")
        text = text.replace('\n','').replace('\t', '')
        text = text.strip(' \t\n\r')
        text = text.replace(u"ž","z")
        text = text.replace(u"č","c")
        text = text.replace(u"ć","c")
        text = text.replace(u"đ","d")
        text = text.replace(u"š","s")

        text = text.replace(u"Ž","Z")
        text = text.replace(u"Č","C")
        text = text.replace(u"Ć","C")
        text = text.replace(u"Đ","D")
        text = text.replace(u"Š","S")

        text = re.sub('[!?@#$"\',.]', '', text)
        text = " ".join(text.split())

        #remove any date time in text
        datum = re.search(r'\d{1,2}.\d{1,2}.\d{4} \d{1,2}:\d{1,2}:\d{1,2}', text)
        if datum is not None:
            text = text.replace(datum.group(), "")

    return text